package cn.b.sky.form.dao.impl;

import cn.b.sky.common.exception.ImeException;
import cn.b.sky.form.dao.BusinessTableRowDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BusinessTableRowDaoJdbcImpl extends JdbcDaoSupport implements BusinessTableRowDao {

	private static final Logger LOGGER = LoggerFactory.getLogger(BusinessTableRowDaoJdbcImpl.class);

	@Override
	public int insertRow(String tableName, Map<String, Object> row) {

		String sql = "insert into " + tableName + " (#FIELDS#) values (#VALUES#)";
		StringBuilder fldSb = new StringBuilder();
		StringBuilder valSb = new StringBuilder();
		List<Object> args = new ArrayList<Object>();
		if(row != null && row.entrySet().size() > 0){
			for(Map.Entry<String, Object> en : row.entrySet()){
				fldSb.append(en.getKey()).append(",");
				valSb.append("?,");
				args.add(en.getValue());
			}
			String fields = fldSb.substring(0, fldSb.lastIndexOf(","));
			String values = valSb.substring(0, valSb.lastIndexOf(","));
			sql = sql.replace("#FIELDS#", fields).replace("#VALUES#", values);
			
			this.getJdbcTemplate().update(sql, args.toArray());
			return (Integer) row.get("id");
		}else{
			LOGGER.error("insert row params is null");
			throw ImeException.newException("ParamsCanNotBeEmpty.M");
		}
	}

	@Override
	public void updateRow(String tableName, Map<String, Object> row) {

		StringBuilder sqlSb = new StringBuilder("update " + tableName + " set ");
		List<Object> args = new ArrayList<Object>();
		if(row != null && row.entrySet().size() > 0){
			for(Map.Entry<String, Object> en : row.entrySet()){
				sqlSb.append(en.getKey()).append("=?").append(",");
				args.add(en.getValue());
			}
			StringBuilder sql = new StringBuilder(sqlSb.substring(0, sqlSb.lastIndexOf(",")));
			sql.append(" where id=?");
			args.add(row.get("id"));
			this.getJdbcTemplate().update(sql.toString(), args.toArray());
		}else{
			LOGGER.error("update row params is null");
			throw ImeException.newException("ParamsCanNotBeEmpty.M");
		}
	}

	@Override
	public void deleteRow(String tableName, int id) {

		String sql = "delete from " + tableName + " where id=?";
		this.getJdbcTemplate().update(sql, id);
	}

	@Override
	public List<Map<String, Object>> selectRows(String tableName,Map<String, Object> parameters) {

		StringBuilder selectSql = new StringBuilder();
		List<Object> args = new ArrayList<Object>();
		selectSql.append("select * from ");
		selectSql.append(tableName);
		if(parameters != null && parameters.entrySet().size() > 0){
			selectSql.append(" where 1=1");
			for(Map.Entry<String, Object> en : parameters.entrySet()){
				selectSql.append(" and ").append(en.getKey()).append("=?");
				args.add(en.getValue());
			}
		}
		return this.getJdbcTemplate().queryForList(selectSql.toString(), args.toArray());
	}

}
